package org.example;

import provider.example.impl.StaticLoggerBinder;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

public final class MyLoggerFactory {
    private static String STATIC_LOGGER_BINDER_PATH = "provider/example/impl/StaticLoggerBinder.class";

    public static Logger getLogger(Class<?> clazz) {
        Logger logger = getLogger(clazz.getName());
        return logger;
    }

    public static Logger getLogger(String name) {
        ILoggerFactory iLoggerFactory = getILoggerFactory();
        return iLoggerFactory.getLogger(name);
    }

    public static ILoggerFactory getILoggerFactory() {
        performInitialization();
        return StaticLoggerBinder.getSingleton().getLoggerFactory();
    }

    private static final void performInitialization() {
        bind();
    }

    private static final void bind() {
        try {
            String msg;
            try {
                Set<URL> staticLoggerBinderPathSet = null;
                staticLoggerBinderPathSet = findPossibleStaticLoggerBinderPathSet();
                reportMultipleBindingAmbiguity(staticLoggerBinderPathSet);


                StaticLoggerBinder.getSingleton();
                reportActualBinding(staticLoggerBinderPathSet);
            } catch (NoClassDefFoundError var7) {
                msg = var7.getMessage();
                System.out.println(msg);
            } catch (NoSuchMethodError var8) {
                msg = var8.getMessage();
                if (msg != null && msg.contains("org.slf4j.impl.StaticLoggerBinder.getSingleton()")) {
                    System.out.println(msg);
                }

                throw var8;
            } catch (Exception var9) {
                throw new IllegalStateException("Unexpected initialization failure", var9);
            }
        } finally {
            System.out.println("clean up");
        }
    }

    static Set<URL> findPossibleStaticLoggerBinderPathSet() {
        Set<URL> staticLoggerBinderPathSet = new LinkedHashSet();

        try {
            ClassLoader loggerFactoryClassLoader = MyLoggerFactory.class.getClassLoader();
            Enumeration paths;
            if (loggerFactoryClassLoader == null) {
                paths = ClassLoader.getSystemResources(STATIC_LOGGER_BINDER_PATH);
            } else {
                paths = loggerFactoryClassLoader.getResources(STATIC_LOGGER_BINDER_PATH);
            }

            while(paths.hasMoreElements()) {
                URL path = (URL)paths.nextElement();
                staticLoggerBinderPathSet.add(path);
            }
        } catch (IOException var4) {
            System.out.println("Error getting resources from path");
        }

        return staticLoggerBinderPathSet;
    }

    private static void reportMultipleBindingAmbiguity(Set<URL> binderPathSet) {
        if (isAmbiguousStaticLoggerBinderPathSet(binderPathSet)) {
            System.out.println("Class path contains multiple SLF4J bindings.");
            Iterator i$ = binderPathSet.iterator();

            while(i$.hasNext()) {
                URL path = (URL)i$.next();
                System.out.println("Found binding in [" + path + "]");
            }

            System.out.println("See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.");
        }

    }

    private static boolean isAmbiguousStaticLoggerBinderPathSet(Set<URL> binderPathSet) {
        return binderPathSet.size() > 1;
    }

    private static void reportActualBinding(Set<URL> binderPathSet) {
        if (binderPathSet != null && isAmbiguousStaticLoggerBinderPathSet(binderPathSet)) {
            System.out.println("Actual binding is of type [" + StaticLoggerBinder.getSingleton().getLoggerFactoryClassStr() + "]");
        }

    }
}
